home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / Vector4.cpp < prev   
C/C++ Source or Header  |  2005-11-18  |  3KB  |  179 lines

  1.  
  2. #include "Vector4.h"
  3.  
  4. namespace peon
  5. {
  6.  
  7.     Vector4::Vector4( float x_, float y_, float z_, float w_ )
  8.     {
  9.         x = x_;
  10.         y = y_;
  11.         z = z_;
  12.         w = w_;
  13.     }
  14.  
  15.     Vector4::~Vector4()
  16.     {
  17.  
  18.     }
  19.  
  20.     void Vector4::set( float x_, float y_, float z_, float w_ )
  21.     {
  22.         x = x_;
  23.         y = y_;
  24.         z = z_;
  25.         w = w_;
  26.     }
  27.  
  28.     float Vector4::length( void )
  29.     {
  30.         return( (float)sqrt( x * x + y * y + z * z + w * w ) );
  31.     }
  32.  
  33.     void Vector4::normalize( void )
  34.     {
  35.         float fLength = length();
  36.  
  37.         x = x / fLength;
  38.         y = y / fLength;
  39.         z = z / fLength;
  40.         w = w / fLength;
  41.     }
  42.  
  43.     // Static utility methods...
  44.  
  45.     static float distance( const Vector4 &v1,  const Vector4 &v2  )
  46.     {
  47.         float dx = v1.x - v2.x;
  48.         float dy = v1.y - v2.y;
  49.         float dz = v1.z - v2.z;
  50.  
  51.         return (float)sqrt( dx * dx + dy * dy + dz * dz );
  52.     }
  53.  
  54.     static float dotProduct( const Vector4 &v1,  const Vector4 &v2 )
  55.     {
  56.         return( v1.x * v2.x + v1.y * v2.y + v1.z * v2.z  );
  57.     }
  58.  
  59.     static Vector4 crossProduct( const Vector4 &v1,  const Vector4 &v2 )
  60.     {
  61.         Vector4 vCrossProduct;
  62.  
  63.         vCrossProduct.x = v1.y * v2.z - v1.z * v2.y;
  64.         vCrossProduct.y = v1.z * v2.x - v1.x * v2.z;
  65.         vCrossProduct.z = v1.x * v2.y - v1.y * v2.x;
  66.  
  67.         return vCrossProduct;
  68.     }
  69.  
  70.     // Operators...
  71.  
  72.     Vector4 Vector4::operator + ( const Vector4 &other )
  73.     {
  74.         Vector4 vResult(0.0f, 0.0f, 0.0f);
  75.  
  76.         vResult.x = x + other.x;
  77.         vResult.y = y + other.y;
  78.         vResult.z = z + other.z;
  79.  
  80.         return vResult;
  81.     }
  82.  
  83.     Vector4 Vector4::operator + ( void ) const
  84.     {
  85.         return *this;
  86.     }
  87.  
  88.     Vector4 Vector4::operator - ( const Vector4 &other )
  89.     {
  90.         Vector4 vResult(0.0f, 0.0f, 0.0f);
  91.  
  92.         vResult.x = x - other.x;
  93.         vResult.y = y - other.y;
  94.         vResult.z = z - other.z;
  95.  
  96.         return vResult;
  97.     }
  98.  
  99.     Vector4 Vector4::operator - ( void ) const
  100.     {
  101.         Vector4 vResult(-x, -y, -z);
  102.  
  103.         return vResult;
  104.     }
  105.  
  106.     Vector4 Vector4::operator * ( const Vector4 &other )
  107.     {
  108.         Vector4 vResult(0.0f, 0.0f, 0.0f);
  109.  
  110.         vResult.x = x * other.x;
  111.         vResult.y = y * other.y;
  112.         vResult.z = z * other.z;
  113.  
  114.         return vResult;
  115.     }
  116.  
  117.     Vector4 Vector4::operator * ( const float scalar )
  118.     {
  119.         Vector4 vResult(0.0f, 0.0f, 0.0f);
  120.  
  121.         vResult.x = x * scalar;
  122.         vResult.y = y * scalar;
  123.         vResult.z = z * scalar;
  124.  
  125.         return vResult;
  126.     }
  127.  
  128.     Vector4 operator * ( const float scalar, const Vector4 &other )
  129.     {
  130.         Vector4 vResult(0.0f, 0.0f, 0.0f);
  131.  
  132.         vResult.x = other.x * scalar;
  133.         vResult.y = other.y * scalar;
  134.         vResult.z = other.z * scalar;
  135.  
  136.         return vResult;
  137.     }
  138.  
  139.     Vector4 Vector4::operator / ( const Vector4 &other )
  140.     {
  141.         Vector4 vResult(0.0f, 0.0f, 0.0f);
  142.  
  143.         vResult.x = x / other.x;
  144.         vResult.y = y / other.y;
  145.         vResult.z = z / other.z;
  146.  
  147.         return vResult;
  148.     }
  149.  
  150.     Vector4& Vector4::operator = ( const Vector4 &other )
  151.     {
  152.         x = other.x;
  153.         y = other.y;
  154.         z = other.z;
  155.  
  156.         return *this;
  157.     }
  158.  
  159.     Vector4& Vector4::operator += ( const Vector4 &other )
  160.     {
  161.         x += other.x;
  162.         y += other.y;
  163.         z += other.z;
  164.  
  165.         return *this;
  166.     }
  167.  
  168.     Vector4& Vector4::operator -= ( const Vector4 &other )
  169.     {
  170.         x -= other.x;
  171.         y -= other.y;
  172.         z -= other.z;
  173.  
  174.         return *this;
  175.     }
  176.  
  177. }
  178.  
  179.